﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(QualityMaterialControl))]
    public class QualityMaterialControlEditor : Editor
    {
        private QualityConfig qualityConfig;
        private SerializedProperty controls;
        private ReorderableList controlList;
        private int currentLevel=1;
        public override void OnInspectorGUI()
        {

            if (this.qualityConfig == null)
            {
                var qualityConfigs = QualityConfig.FindConfigs();
                if (qualityConfigs.Length == 0)
                {
                    EditorGUILayout.HelpBox("There has no quality config in the project.", MessageType.Error);
                    this.qualityConfig = null;
                }
                else
                {
                    if (qualityConfigs.Length > 1)
                    {
                        EditorGUILayout.HelpBox("There are more than one quality config.", MessageType.Warning);
                    }
                    this.qualityConfig = qualityConfigs[0];
                }
            }

            if (this.qualityConfig != null)
            {
                var levelCount = this.qualityConfig.GetLevelCount();
                var qualityMenu = new string[levelCount];
                for (int i = 0; i < levelCount; ++i)
                {
                    var level = this.qualityConfig.GetLevel(i);
                    qualityMenu[i] = level.Name;
                }

                //var currentLevel = QualityConfig.QualityLevel;
                //EditorGUI.BeginChangeCheck();
                //currentLevel = EditorGUILayout.Popup( "Set Global Quality:", currentLevel, qualityMenu);
                //if (EditorGUI.EndChangeCheck())
                //{
                //    QualityConfig.QualityLevel = currentLevel;
                //}

                EditorGUILayout.Space();
                EditorGUILayout.Space();
                EditorGUI.BeginChangeCheck();
                currentLevel = EditorGUILayout.Popup("Test Current Quality:", currentLevel, qualityMenu);
                if (EditorGUI.EndChangeCheck())
                {
                    var ctrl = target as QualityMaterialControl;
                    //ctl.SetQualityLevel(currentLevel);
                    QualityConfig.QualityLevel = currentLevel; //美术测试用
                }

                EditorGUILayout.Space();
                EditorGUILayout.Space();
            }

            // Draw the editor.
            this.serializedObject.Update();
            this.controlList.DoLayoutList();
            this.serializedObject.ApplyModifiedProperties();
        }

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }

            this.controls = this.serializedObject.FindProperty("controls");
            this.controlList = new ReorderableList(serializedObject, this.controls);
            this.controlList.drawHeaderCallback = OnDrawHead;
            this.controlList.elementHeight = EditorGUIUtility.singleLineHeight;
            this.controlList.drawElementCallback = this.DrawControl;

        }
        private void OnDrawHead(Rect rect)
        {
            Rect position = new Rect(rect.x + 15f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect position2 = new Rect(rect.x + 15f + rect.width / 2f, rect.y, rect.width - position.width, EditorGUIUtility.singleLineHeight);
            GUI.Label(position, "Material");
            GUI.Label(position2, "QualityLevel Enable");
        }

        private void DrawControl(Rect rect, int index, bool isActive, bool isFocused)
        {
            var element = this.controls.GetArrayElementAtIndex(index);

            var target = element.FindPropertyRelative("Target");
            var enabledLevels = element.FindPropertyRelative("EnabledLevels");

            var rectLine = new Rect(rect.x, rect.y, rect.width / 3f, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(rectLine, target, GUIContent.none);

            if (this.qualityConfig == null)
            {
                var origin = GUI.color;
                GUI.color = Color.red;
                GUI.Label(rectLine, "Missing quality config.");
                GUI.color = origin;
            }
            else
            {
                int levelCount = this.qualityConfig.GetLevelCount();
                enabledLevels.arraySize = levelCount;

                float itemWidth = (rect.width - rectLine.width) / levelCount;
                float startX = rect.x + rectLine.width + 6f;

                // Draw the toggle
                for (int i = 0; i < levelCount; ++i)
                {
                    var level = this.qualityConfig.GetLevel(i);
                    GUIContent levelname = new GUIContent(level.Name);
                    var rectItem = new Rect(startX + itemWidth * i, rect.y, itemWidth, EditorGUIUtility.singleLineHeight);

                    var levelEnabled = enabledLevels.GetArrayElementAtIndex(i);
                    levelEnabled.boolValue = GUI.Toggle(rectItem, levelEnabled.boolValue, levelname);
                }
            }
        }
    }
}

